﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Smartmanufact
{
    public partial class ADD_customer : Form
    {
        public ADD_customer()
        {
            InitializeComponent();
            // 为当前客户分配一ID号
            for(int i=1;i<=99999999;i++)
            {
                if(is_exit(i)==false)
                {
                    // 如果此id不存在，就分配为此id
                    label_id.Text = i.ToString().PadLeft(8, '0');
                    break;
                }
            }

        }

        private bool is_exit(int id)
        {
            // 判断id是否存在
            bool exit_is = false;
            DataSet ds = MainView.mysql.sql_search_database("select * from customertable");
            int row_count = ds.Tables[0].Rows.Count;
            for (int i = 0; i < row_count;i++ )
            {
                if(id==int.Parse(ds.Tables[0].Rows[i][0].ToString()))
                {
                    return true;
                }
            }
            return exit_is;
        }

        private void ADD_customer_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox_name.Text.Trim() == "") { MessageBox.Show("姓名不能为空"); return; }
                string id = label_id.Text.Trim();
                string name = textBox_name.Text.Trim();
                string company = textBox_company.Text.Trim();
                string phone = textBox_phone.Text.Trim();
                string email = textBox_email.Text.Trim();
                MainView.mysql.excute_sql("insert into customertable values('"+id+"','"+name+"','"+company+"','"+phone+"','"+email+"')");
                this.Dispose();
            }
            catch { }
        }
    }
}
